package com.jy.blog.controller;

import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jy.blog.model.Admin;
import com.jy.blog.model.Article;
import com.jy.blog.model.User;
import com.jy.blog.service.ArticleService;
import com.jy.blog.service.impl.ArticleServiceImpl;

/**
 * Servlet implementation class ArticleServlet
 */
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 private ArticleService service=new ArticleServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		
		if(action.equals("list")){
			list(request,response);
		}else  if(action.equals("update")){
			update(request,response);
		}else if(action.equals("del")){
			del(request,response);
		}else if(action.equals("doUpdate")){
			doUpdate(request,response);
		}else if(action.equals("logut")){
			logut(request,response);
		}else if(action.equals("getProduct")){
			getProduct(request,response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		  if(action.equals("doUpdate")){
				doUpdate(request, response);
			}else if(action.equals("add")){
				add(request, response);
			}
	}
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List <Article> articleList =service.selectAllArticle();
		request.setAttribute("articleList", articleList);
		request.getRequestDispatcher("list.jsp").forward(request, response);
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Article article=new Article();
		article.setArtTitle(request.getParameter("title"));
		article.setArtContent(request.getParameter("content"));
		article.setAuthor((String) request.getSession().getAttribute("userName"));
		article.setArtDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		article.setAdPhoto(request.getParameter("adPhoto"));
		service.add(article);
		
		response.sendRedirect(request.getContextPath()+"/admin/article?action=list");
	
	}
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.valueOf(request.getParameter("id"));
		Article article=service.selectById(id);
		request.setAttribute("article", article);
		request.getRequestDispatcher("article-update.jsp").forward(request, response);
			
			
		}
	protected void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Article article=new Article();
		article.setArtId(Integer.valueOf(request.getParameter("id")));
		article.setArtTitle(request.getParameter("title"));
		article.setArtContent(request.getParameter("content"));
		article.setAdPhoto(request.getParameter("adPhoto"));
		article.setArtDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		service.update(article);
		response.sendRedirect(request.getContextPath()+"/admin/article?action=list");
		
	}
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.valueOf(request.getParameter("id"));
		service.del(id);
		response.sendRedirect(request.getContextPath()+"/admin/article?action=list");
		
	}
	protected void logut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user=new User();
		String action=request.getParameter("action");
		user.setUserName(request.getParameter("userName"));
		if(action.equals("logut")){
			HttpSession session=request.getSession();
			session.removeAttribute("userName");
			response.sendRedirect(request.getContextPath()+"/login.jsp");
		}
		
	}
	protected void getProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List <Article> articleList =service.selectAllArticle();
		request.setAttribute("articleList", articleList);
		request.getRequestDispatcher("list.jsp").forward(request, response);
		
	}
}
